/*
 * This file is part of animus, a GUI for the command line app "GHost++"
 *
 * http://code.google.com/p/animus/
 *
 * Copyright (C) 2008-2009 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */


using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Xml.Serialization;

namespace animus
{
    public class ConsoleTriggerFontChange : ConsoleTrigger
    {
        public override string GetDescription()
        {
            return "Change the style of text output";
        }
        protected static RichTextBox m_TextContainer = null;
        public static void SetTextContainer(RichTextBox target)
        {
            m_TextContainer = target;
        }
        protected System.Drawing.Color m_Color = Color.Red;
        [Category("Text Highlight")]
        [Description("The desired color of the text matched by this trigger")]
        [XmlIgnoreAttribute]
        public System.Drawing.Color Color
        {
            get { return m_Color; }
            set { m_Color = value; }
        }
        //used for serialization
        [XmlElement("Color")]
        [Browsable(false)]
        public string ColorSerializer
        {
            get { return ColorTranslator.ToHtml(m_Color); }
            set { m_Color = ColorTranslator.FromHtml(value); }
        }
        protected bool m_Bold = false;
        [Category("Text Highlight")]
        [Description("Prints the matched text in bold letters")]
        public bool Bold
        {
            get { return m_Bold; }
            set { m_Bold = value; }
        }
        public ConsoleTriggerFontChange()
            : base()
        {
            //default constructor
        }
        public ConsoleTriggerFontChange(string expression, System.Drawing.Color color, bool bold)
            : base(expression)
        {
            m_Color = color;
            m_Bold = bold;
        }
        public override string GetParameters()
        {
            return m_Color + " - Bold: " + m_Bold;
        }
        protected override void Triggered(string line, Match match)
        {
            //suppress output, we handle it ourselves
            m_SuppressOutput = true;
            //insert linebreak
            m_TextContainer.AppendText(Environment.NewLine);
            //insert text not matches by expression (before)
            m_TextContainer.AppendText(line.Substring(0, match.Index));

            //set text color and font
            m_TextContainer.Select(m_TextContainer.TextLength, 0);
            m_TextContainer.SelectionColor = m_Color;
            if (m_Bold)
                m_TextContainer.SelectionFont = new Font(m_TextContainer.Font, FontStyle.Bold);
            //append custom formatted matched text
            m_TextContainer.AppendText(line.Substring(match.Index, match.Length));

            //restore original font and color
            m_TextContainer.Select(m_TextContainer.TextLength, 0);
            m_TextContainer.SelectionColor = m_TextContainer.ForeColor;
            m_TextContainer.SelectionFont = m_TextContainer.Font;

            //insert text not matched by expression (after)
            m_TextContainer.AppendText(line.Substring(match.Index + match.Length, line.Length - (match.Index + match.Length)));
        }
    }
}
